// pages/login/login.js
//获取应用实例
const app = getApp()
const util = require('../../utils/util.js')
// const md5 = require('../../utils/md5.js')

Page({

  /**
   * 页面的初始数据1
   */
  data: {
    username: null,
    password: null
  },

  username: function(e) {
    this.data.username = e.detail.value
  },

  password: function(e) {
    this.data.password = e.detail.value
  },

  submit: util.throttle(function(e) {
    let username = this.data.username
    let password = this.data.password
    if (username == null || username == "" || password == null || password == "") {
      wx.showModal({
        content: '用户名密码不能为空',
        showCancel: false,
        success: function(res) {
          if (res.confirm) {
            // console.log('用户点击确定')
          }
        }
      })
    } else {
      // 绑定微信并登录
      //密码进行MD5加密
      //password = md5.md5(password) ;
      wx.showLoading({
        title: '登录中',
        mask: true
      })
      wx.request({
        url: app.userUrl + '/WeChat/AccountManagement/bindUserWithWeChat',
        method: 'POST',
        data: {
          username: username,
          password: password,
          weChatId: app.globalData.wechatId
        },
        dataType: 'json',
        success: (res) => {
          wx.hideLoading()
          if (res.data.result == "success") {
            wx.request({
              url: app.userUrl + '/WeChat/weChatSystem/getInspectInfo',
              method: 'POST',
              data: {
                schoolId: res.data.departmentId,
                userId:  res.data.id,
                identityId:  res.data.identityId,
              },
              dataType: 'json',
              success: res => {
                wx.setStorageSync('allList', res.data)
              }
            })
            //跳转到主页面
            app.globalData.userId = res.data.id
            app.globalData.username = res.data.username
            app.globalData.department = res.data.department
            app.globalData.departmentId = res.data.departmentId
            app.globalData.inspectModel = res.data.inspectModel
            app.globalData.identity = res.data.identity
            app.globalData.identityId = res.data.identityId
            app.globalData.userType = res.data.userType
            app.globalData.departmentType = res.data.departmentType
            app.globalData.realname = res.data.realName
            app.globalData.phone = res.data.phone
            app.globalData.position = res.data.position
            app.globalData.parent = res.data.parent
            wx.switchTab({
              url: '/pages/index/index'
            })
          } else {
            wx.showModal({
              content: res.data.error + '请重试！',
              showCancel: false,
              success: function(res) {
                if (res.confirm) {
                  // console.log('用户点击确定')
                }
              }
            })
          }
        },
        fail: res => {
          wx.showToast({
            title: '网络异常！',
            image: '/images/warning.png'
          })
        }
      })
    }
  }),

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

    // app.wechatIdCallback = res => {
    //   if (res.data.openid) {
    //     app.globalData.wechatId = res.data.openid
    //     // 回调成功后
    //     wx.request({
    //       url: app.baseUrl + '/AccountManagement/getUserByWeChatId',
    //       method: 'POST',
    //       data: {
    //         wechatId: app.globalData.wechatId
    //       },
    //       dataType: 'json',
    //       success: res => {
    //         if (res.data.length != 0) {
    //           app.globalData.username = res.data[0].username
    //           app.globalData.departmentId = res.data[0].departmentId
    //           app.globalData.identityId = res.data[0].identityId
    //           wx.switchTab({
    //             url: '/pages/index/index'
    //           })
    //         }
    //       }
    //     })
    //   }
    // }
  },

  signIn: util.throttle(function() {
    util.scanQRCode("login")
  })
})